package com.koron.sinoprobe.web.subpackage.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.common.core.util.CodeTools;
import com.koron.sinoprobe.web.ledger.bean.entity.SpProject;
import com.koron.sinoprobe.web.ledger.mapper.SpProjectMapper;
import com.koron.sinoprobe.web.subpackage.bean.SubPackage;
import com.koron.sinoprobe.web.subpackage.bean.query.SubPackageQuery;
import com.koron.sinoprobe.web.subpackage.bean.vo.SubPackageVO;
import com.koron.sinoprobe.web.subpackage.mapper.SubPackageMapper;
import com.koron.sinoprobe.web.subpackage.service.SubPackageService;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

@Slf4j
@Service
public class SubPackageServiceImpl implements SubPackageService {

    @TaskAnnotation("saveOrUpdateSubPackage")
    @Override
    public SubPackage saveOrUpdateSubPackage(SessionFactory factory, SubPackage subPackage) {
        SubPackageMapper mapper = factory.getMapper(SubPackageMapper.class);

        if (StrUtil.isEmpty(subPackage.getId())){
            SpProjectMapper spProjectMapper = factory.getMapper(SpProjectMapper.class);
            SpProject spProject = spProjectMapper.queryById(subPackage.getProjectId());
            String projectCode = spProject.getCode();
            Assert.notNull(projectCode, "台账编码不能为空！");
            subPackage.setId(CodeTools.getCode32());
            //subPackage.setCode(CodeTools.getCurrAppNameCodeNo("%s[%s]分包%s号","yyyy",3));
            subPackage.setCode(CodeTools.getCodeNo(projectCode + "_" + SubPackage.PREFIX + "_", "yyyy",3));
            mapper.addSubPackage(subPackage);
        }else {
            updateSubPackage(factory, subPackage);
        }

        return subPackage;
    }

    @TaskAnnotation("getSubPackageById")
    @Override
    public SubPackageVO getSubPackageById(SessionFactory factory, String id) {
        SubPackageMapper mapper = factory.getMapper(SubPackageMapper.class);
        SubPackageVO subPackage = mapper.getSubPackageById(id);
        return subPackage;
    }

    @TaskAnnotation("updateSubPackage")
    @Override
    public SubPackage updateSubPackage(SessionFactory factory, SubPackage subPackage) {
        SubPackageMapper mapper = factory.getMapper(SubPackageMapper.class);
        mapper.updateSubPackage(subPackage);
        return subPackage;
    }

    @TaskAnnotation("deleteSubPackage")
    @Override
    public Integer deleteSubPackage(SessionFactory factory, String id) {
        SubPackageMapper mapper = factory.getMapper(SubPackageMapper.class);
        int result = mapper.deleteSubPackage(id);
        return result;
    }

    @TaskAnnotation("page")
    @Override
    public PageInfo page(SessionFactory factory, PageQuery pageQuery, SubPackageQuery subPackageQuery) {
        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize(), pageQuery.getOrderBy(true));
        SubPackageMapper mapper = factory.getMapper(SubPackageMapper.class);
        List<SubPackage> subPackages  = mapper.selectList(subPackageQuery);
        return new PageInfo(subPackages);
    }

}
